﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using UserCtrl;

namespace GRWL.BZSC.UI
{
    public partial class QueryStockAmountFrm : BaseFrm
    {
        private GRWL.BZSC.Common.EnumCommon.OptionType CurrentType { get; set; }
        
        public QueryStockAmountFrm()
        {
            InitializeComponent();                    
        }

        private void QueryStockAmountFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy = new GRWL.BZSC.UI.BaseDataSvc.BaseDataClient();
            this.BaseDataProxy.Open();

            gvDetail.CellFormatting += new DataGridViewCellFormattingEventHandler(gvDetail_CellFormatting);
            
            initTreeView();
            initGridView();

            btnSearch.Click += new EventHandler(btnSearch_Click);
            btnPrint.Click += new EventHandler(btnPrint_Click);
            btnClose.Click += new EventHandler(btnClose_Click);
        }

        void gvDetail_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            Common.CellFormatColor(sender, e, Common.BillOperateType.View);
        }

        private void initTreeView()
        {
            UTreeView tv = null;

            this.CurrentType = GRWL.BZSC.Common.EnumCommon.OptionType.仓库分类;
            tv = this.uTreeView2;
            createTree(tv);

            this.CurrentType = GRWL.BZSC.Common.EnumCommon.OptionType.物料分类;
            tv = this.uTreeView1;
            createTree(tv);

        }

        private void createTree(UTreeView tv)
        {
            TB_Options[] options = this.BaseDataProxy.GetOptionTree(this.CurrentType.ToString());
            Common.BindOptionTree(tv, options, this.CurrentType.ToString());
            if (this.CurrentType == GRWL.BZSC.Common.EnumCommon.OptionType.仓库分类)
            {
                TB_Depot[] ary = this.BaseDataProxy.FindDepots(string.Empty, string.Empty, null);
                if (ary != null)
                    loadDepot(ary);
            }

        }

        private void loadDepot(TB_Depot[] ary)
        {
            foreach (TreeNode node in this.uTreeView2.Nodes[0].Nodes)
            {
                if (node.ImageIndex == 2)
                {
                    int category = Convert.ToInt32(node.Name);
                    var query = from q in ary where q.type == category select q;
                    if (query != null)
                    {
                        foreach (TB_Depot d in query)
                        {
                            TreeNode child = new TreeNode();
                            child.Name = d.id.ToString();
                            child.Tag = d.code;
                            child.Text = "[" + d.code + "]" + d.name;
                            child.ImageIndex = 3;
                            node.Nodes.Add(child);
                        }
                    }
                }
            }
        }

        private void initGridView()
        {
            addColumn("materialCode", "材料编号", true);
            addColumn("materialName", "材料名称", true);
            addColumn("specs", "规格", true);
            addColumn("bNum", "上月结存", false);
            addColumn("nNum", "当前数量", false);
            addColumn("price", "平均单价", true);
            addColumn("amount", "仓库金额", true);
            addColumn("depotName", "仓库", true);
            addColumn("categoryName", "分类", true);
            addColumn("unit", "单位", true);
        }

        private void addColumn(string TheFieldName, string TheCaption, bool FieldVisible)
        {
            gvDetail.Columns.Add(TheFieldName, TheCaption);
            gvDetail.Columns[TheFieldName].DataPropertyName = TheFieldName;
            gvDetail.Columns[TheFieldName].Visible = FieldVisible;

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int? category = null;
            int? depotCategory = null;
            int? depot = null;

            if (uTabControl1.SelectedIndex == 0)
            {
                if (uTreeView1.SelectedNode != null)
                {
                    category = Common.StringToInt(uTreeView1.SelectedNode.Name);
                }
            }
            else
            {
                if (uTabControl1.SelectedIndex == 1)
                {
                    if (uTreeView2.SelectedNode != null)
                    {
                        if (uTreeView2.SelectedNode.ImageIndex == 2)
                            depotCategory = Common.StringToInt(uTreeView2.SelectedNode.Name);
                        else
                            if (uTreeView2.SelectedNode.ImageIndex == 3)
                                depot = Common.StringToInt(uTreeView2.SelectedNode.Name);
                    }
                }
            }

            QStockAmount[] querys = BaseDataProxy.QueryStockAmount(category, depotCategory, depot);
            if (querys == null)
                return;

            try
            {
                Cursor.Current = Cursors.WaitCursor;
                gvDetail.SuspendLayout();
                gvDetail.Rows.Clear();
                foreach (QStockAmount q in querys)
                {
                    gvDetail.Rows.Add(q.Code, q.Name, q.Specs, q.BNum, q.NNum, q.Price,
                        q.Amount, q.Depot, q.Category, q.Unit);
                }
                gvDetail.AutoResizeColumns();
            }
            finally
            {
                gvDetail.ResumeLayout();
                Cursor.Current = Cursors.Default;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
